package com.ocom.mqsimple.business.bigfile;

import com.alibaba.fastjson.JSON;
import com.ocom.common.request.human.UpdateUserFaceVo;
import com.ocom.mqsimple.config.BigFileMqConfig;
import com.ocom.mqsimple.feign.HumanClientService;
import com.ocom.mqsimple.feign.MinioClientService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Component
public class BigFileConsumerMqListener {

	private Logger logger = LoggerFactory.getLogger(BigFileConsumerMqListener.class);

	@Value("${save.path}")
	String path;

	@Autowired
	MinioClientService minioClientService;

	@Autowired
	HumanClientService humanClientService;

//	@Resource
//	RedisUtil redisUtil;


	/**
	 * 把该监听命名为A监听
	 * 监听队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues = BigFileMqConfig.BIG_FILE_QUEUE)
	public void process(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		logger.info("BIG_FILE_QUEUE 消费中");
		logger.info("message【{}】",message);
		String humanMessage = new String(message.getBody(), StandardCharsets.UTF_8);
		//TODO 数据库操作样例
		logger.info("BIG_FILE_QUEUE-start:【{}】",humanMessage);
		logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());

		UpdateUserFaceVo bigFileRequest= JSON.parseObject(humanMessage, UpdateUserFaceVo.class);

		try {
			//更改用户头像
			UpdateUserFaceVo updateUserFaceVo=new UpdateUserFaceVo();
			updateUserFaceVo.setComId(bigFileRequest.getComId());
			updateUserFaceVo.setType(bigFileRequest.getType());
			updateUserFaceVo.setPPhotoUrl(bigFileRequest.getPPhotoUrl());
			updateUserFaceVo.setParam(bigFileRequest.getParam());
			logger.info("更新人脸图片：【{}】",updateUserFaceVo);
			humanClientService.updateUserFace(updateUserFaceVo);

		} catch (Exception e) {
			logger.error("消息处理失败:{{}}",humanMessage);
			logger.info("出现异常队列消息开始进入死信队列{{}}",e);
		}

		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		logger.info("BIG_FILE_QUEUE-end:【{}】",humanMessage);
		logger.info("System.currentTimeMillis()【{}】",System.currentTimeMillis());
	}

	/**
	 * 把该监听命名为B监听
	 * 监听死信队列  远程服务支付接口发送消息  该队列会接受
	 * [message, headers, channel]
	 * @return {@link }
	 * @throws
	 */
	@RabbitListener(queues =  BigFileMqConfig.BIG_FILE_DIE_QUEUE)
	public void processForDie(Message message, @Headers Map<String, Object> headers, Channel channel) throws Exception {
		//死信队列放弃
		channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);

	}


	/**
	 * 获取文件夹下所有文件的路径
	 *
	 * @param folderPath
	 * @return
	 */
	public static List<String> getFilePath(String folderPath) {
		File folder = new File(folderPath);
		List<String> filePathList = new ArrayList<>();
		String rootPath;
		if (folder.exists()) {
			String[] fileNameList = folder.list();
			if (null != fileNameList && fileNameList.length > 0) {
				if (folder.getPath().endsWith(File.separator)) {
					rootPath = folder.getPath();
				} else {
					rootPath = folder.getPath() + File.separator;
				}
				for (String fileName : fileNameList) {
					String newPath= rootPath + fileName;
					filePathList.add(newPath);
				}
			}
		}
		return filePathList;
	}


	public static void deleteDir(File file) {
		if (file.isDirectory()) {
			for (File f : file.listFiles())
				deleteDir(f);
		}
		file.delete();
	}

	/**
	 * 获取所有XML文件
	 * @param path 文件路径
	 * @param fileList 存放所有XML文件的列表
	 */

	public void getImageFiles(String path, List<File> fileList) {
		File file = new File(path);
		File[] files = file.listFiles();
		logger.info("读取到文件列表,【{}】",files);
		for (File fileIndex : files) {
			if (!fileIndex.exists()) {
				continue;
			//	throw new NullPointerException("Cannot find " + fileIndex);
			} else if (fileIndex.isFile()) {
				fileList.add(fileIndex);
			} else {
				if (fileIndex.isDirectory()) {
					getImageFiles(fileIndex.getAbsolutePath(), fileList);
				}
			}
		}
	}


}
